//
//  OrderInfoViewController.m
//  DaiShengInvest
//
//  Created by 张睿 on 2017/7/24.
//  Copyright © 2017年 davinci. All rights reserved.
//

#import "OrderInfoViewController.h"
#import "ChangeViewController.h"
#import <SocketRocket.h>

@interface OrderInfoViewController ()<SRWebSocketDelegate>

@property (nonatomic, weak)UIImageView * backImageView;
@property (nonatomic, weak)UILabel * floatTypeLabel;
@property (nonatomic, weak)UILabel * floatTypeValueLabel;
@property (nonatomic, weak)UILabel * buyTypeLabel;
@property (nonatomic, weak)UIView * oneLineView;
@property (nonatomic, weak)UILabel * trandInfoLabel;
@property (nonatomic, weak)UILabel * trandTypeLabel;
@property (nonatomic, weak)UILabel * trandTypeValueLabel;
@property (nonatomic, weak)UILabel * orderNumberLabel;
@property (nonatomic, weak)UILabel * orderNumberValueLabel;
@property (nonatomic, weak)UILabel * pendingTypeLabel;
@property (nonatomic, weak)UILabel * pendingTypeValueLabel;
@property (nonatomic, weak)UILabel * pendingPriceLabel;
@property (nonatomic, weak)UILabel * pendingPriceValueLabel;
@property (nonatomic, weak)UILabel * buyPriceLabel;
@property (nonatomic, weak)UILabel * buyPriceValueLabel;
@property (nonatomic, weak)UILabel * nowPriceLabel;
@property (nonatomic, weak)UILabel * nowPriceValueLabel;
@property (nonatomic, weak)UILabel * stopLosePriceLabel;
@property (nonatomic, weak)UILabel * stopLosePriceValueLabel;
@property (nonatomic, weak)UILabel * stopWinPriceLabel;
@property (nonatomic, weak)UILabel * stopWinPriceValueLabel;
@property (nonatomic, weak)UILabel * pendingTimeLabel;
@property (nonatomic, weak)UILabel * pendingTimeValueLabel;
@property (nonatomic, weak)UILabel * stopTimeLabel;
@property (nonatomic, weak)UILabel * stopTimeValueLabel;
@property (nonatomic, weak)UIView * twoLineView;
@property (nonatomic, weak)UIButton * settingButton;
@property (nonatomic, weak)UIButton * cancelButton;

@property (nonatomic, copy)NSString * typeString;


@property (nonatomic, strong)SRWebSocket * webSocket;

@property (nonatomic, strong)NSTimer * countDownTimer;

@property (nonatomic, strong)NSArray * fiveArray;
@property (nonatomic, strong)NSArray * threeArray;
@property (nonatomic, strong)NSArray * twoArray;

@end

@implementation OrderInfoViewController

//倒计时总的秒数
static NSInteger  secondsCountDown = 2;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.typeString = @"Buy";
    
    self.fiveArray = @[@"EURUSDapp",@"NZDUSDapp",@"EURCADapp",@"USDCADapp",@"EURNZDapp",@"AUDNZDapp",@"AUDUSDapp",@"AUDCADapp",@"GBPCHFapp",@"EURAUDapp",@"EURGBPapp",@"CADCHFapp",@"GBPAUDapp",@"EURCHFapp",@"USDCHFapp",@"GBPCADapp",@"GBPNZDapp",@"AUDCHFapp",@"GBPUSDapp"];
    self.threeArray = @[@"XAGUSDapp",@"GBPJPYapp",@"AUDJPYapp",@"USDJPYapp",@"EURJPYapp",@"CADJPYapp",@"NZDJPYapp"];
    self.twoArray = @[@"XAUUSDapp"];
    
    [self setupNavBar];
    [self setupUI];
}


- (void)viewWillAppear:(BOOL)animated{
    
    _webSocket.delegate = nil;
    [_webSocket close];
    
    _webSocket = [[SRWebSocket alloc] initWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"ws://103.20.249.35:9506"]]];
    
    
    _webSocket.delegate = self;
    [_webSocket open];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSDictionary * parameters = @{@"orderId":self.positionId};
    
    [[NetworkTool sharedTools] POST:@"symbols/positions-detail" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
//        NSLog(@"==%@",responseObject);
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        NSDictionary * paramDict = responseObject[@"param"];
        NSDictionary * priceDict = paramDict[@"price"];
        
        self.typeString = paramDict[@"orderDirection"];
        
        if ([self.typeString isEqualToString:@"Buy"]) {
            
            self.buyTypeLabel.text = @"买";
            self.nowPriceValueLabel.text = [NSString stringWithFormat:@"%@",priceDict[@"ask"]];
            self.buyTypeLabel.textColor = RED_COLOR;
        }else{
            
            self.buyTypeLabel.text = @"卖";
            self.nowPriceValueLabel.text = [NSString stringWithFormat:@"%@",priceDict[@"bid"]];
            
            self.buyTypeLabel.textColor = GREEN_COLOR;
        }
        
        float floatCount = [paramDict[@"pnl"] floatValue];
        
        if (floatCount >= 0) {
            self.floatTypeValueLabel.text = [NSString stringWithFormat:@"+$%.2f",floatCount];
            self.floatTypeValueLabel.textColor = RED_COLOR;
        }else{
            NSString * floatString = [NSString stringWithFormat:@"%.2f",floatCount];
            floatString = [floatString substringFromIndex:1];
            
            self.floatTypeValueLabel.text = [NSString stringWithFormat:@"-$%@",floatString];
            self.floatTypeValueLabel.textColor = GREEN_COLOR;
        }
        
//        self.floatTypeValueLabel.text = [NSString stringWithFormat:@"%@",paramDict[@"pnl"]];
        self.trandTypeValueLabel.text = [NSString stringWithFormat:@"%@",paramDict[@"symbol"]];
        self.orderNumberValueLabel.text = self.positionId;
        self.pendingTypeValueLabel.text = [NSString stringWithFormat:@"%@",paramDict[@"volume"]];//
        float swap = [paramDict[@"swap"] floatValue];
        
        self.pendingPriceValueLabel.text = [NSString stringWithFormat:@"%.2f",swap];
        //交易金额
//        self.pendingPriceValueLabel.text = [NSString stringWithFormat:@"%@",paramDict[@"openPrice"]];
        
        BOOL isFivebool = [self.fiveArray containsObject:self.trandTypeValueLabel.text];
        BOOL isThreebool = [self.threeArray containsObject:self.trandTypeValueLabel.text];
        BOOL isTwobool = [self.twoArray containsObject:self.trandTypeValueLabel.text];
        
        float openPrice = [paramDict[@"openPrice"] floatValue];
        float stopLoss = [paramDict[@"stopLoss"] floatValue];
        float takeProfit = [paramDict[@"takeProfit"] floatValue];
//        NSLog(@"====%f",takeProfit);
        NSString * stopLose1 = [NSString stringWithFormat:@"%@",paramDict[@"stopLoss"]];
        NSString * stopWin1 = [NSString stringWithFormat:@"%@",paramDict[@"takeProfit"]];
        
        
        if (isFivebool) {
            
            self.buyPriceValueLabel.text = [NSString stringWithFormat:@"%.5f",openPrice];
            
            if ([stopLose1 isEqualToString:@"0"]) {
                self.stopLosePriceValueLabel.text = @"未设置";
            }else{
                self.stopLosePriceValueLabel.text = [NSString stringWithFormat:@"%.5f",stopLoss];
            }
            
            if ([stopWin1 isEqualToString:@"0"]) {
                self.stopWinPriceValueLabel.text = @"未设置";
            }else{
                self.stopWinPriceValueLabel.text = [NSString stringWithFormat:@"%.5f",takeProfit];;
            }
        }
        
        if (isThreebool) {
            
            self.buyPriceValueLabel.text = [NSString stringWithFormat:@"%.3f",openPrice];
 
            if ([stopLose1 isEqualToString:@"0"]) {
                self.stopLosePriceValueLabel.text = @"未设置";
            }else{
                self.stopLosePriceValueLabel.text = [NSString stringWithFormat:@"%.3f",stopLoss];
            }
            
            if ([stopWin1 isEqualToString:@"0"]) {
                self.stopWinPriceValueLabel.text = @"未设置";
            }else{
                self.stopWinPriceValueLabel.text = [NSString stringWithFormat:@"%.3f",takeProfit];;
            }
        }
        
        if (isTwobool) {
            
            self.buyPriceValueLabel.text = [NSString stringWithFormat:@"%.2f",openPrice];
            
            if ([stopLose1 isEqualToString:@"0"]) {
                self.stopLosePriceValueLabel.text = @"未设置";
            }else{
                self.stopLosePriceValueLabel.text = [NSString stringWithFormat:@"%.2f",stopLoss];
            }
            
            if ([stopWin1 isEqualToString:@"0"]) {
                self.stopWinPriceValueLabel.text = @"未设置";
            }else{
                self.stopWinPriceValueLabel.text = [NSString stringWithFormat:@"%.2f",takeProfit];;
            }
        }
        
        //买入价格
//        self.buyPriceValueLabel.text = [NSString stringWithFormat:@"%@",paramDict[@"openPrice"]];
        //现在价格
//        self.nowPriceValueLabel.text = [NSString stringWithFormat:@"%@",paramDict[@""]];
        
        
//        self.stopWinPriceValueLabel.text = [NSString stringWithFormat:@"%@",paramDict[@"takeProfit"]];
        NSString * timeStr = [NSString stringWithFormat:@"%@",paramDict[@"openTime"]];
//        NSLog(@"==%@",timeStr);
        
        NSArray * timeArray = [timeStr componentsSeparatedByString:@"T"];
        NSString * timeString = [timeArray.lastObject substringToIndex:8];
//        NSLog(@"==%@",timeString);
        
        self.pendingTimeValueLabel.text = [NSString stringWithFormat:@"%@ %@",timeArray.firstObject,timeString];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
    }];    
    
    secondsCountDown = 2;
    
    [self setupDownTimer];
}


- (void)setupDownTimer{
    
    _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownAction) userInfo:nil repeats:YES];
}


//定时器的方法
- (void)countDownAction{
    
    secondsCountDown --;
    
    //当倒计时到0时做需要的操作，button变成可选状态
    if(secondsCountDown == 0){
        
        [_countDownTimer invalidate];
        
        NSDictionary * parameters = @{@"orderId":self.positionId};
        
        [[NetworkTool sharedTools] POST:@"symbols/positions-detail" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
            
            NSDictionary * paramDict = responseObject[@"param"];
            
            float floatCount = [paramDict[@"pnl"] floatValue];
            
            if (floatCount >= 0) {
                self.floatTypeValueLabel.text = [NSString stringWithFormat:@"+$%.2f",floatCount];
                self.floatTypeValueLabel.textColor = RED_COLOR;
            }else{
                NSString * floatString = [NSString stringWithFormat:@"%.2f",floatCount];
                floatString = [floatString substringFromIndex:1];
                
                self.floatTypeValueLabel.text = [NSString stringWithFormat:@"-$%@",floatString];
                self.floatTypeValueLabel.textColor = GREEN_COLOR;
            }
            
            secondsCountDown = 2;
            
            [self setupDownTimer];
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            
        }];
        
        
    }
}


- (void)setupNavBar{
    
    self.title = @"订单详情";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToAccountView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}

- (void)setupUI{
    
    UIImageView * backImage = [[UIImageView alloc] init];
    
    backImage.image = [UIImage imageNamed:@"背景"];
    
    self.backImageView = backImage;
    
    [self.view addSubview:self.backImageView];
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.trailing.mas_equalTo(self.view);
    }];
    
    
    UILabel * floatTypeLabel = [UILabel new];
    
    floatTypeLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    floatTypeLabel.textColor = ColorWithRGB(0xffffff);
    floatTypeLabel.textAlignment = NSTextAlignmentLeft;
    floatTypeLabel.text = @"浮动盈亏($)";
    
    self.floatTypeLabel = floatTypeLabel;
    
    [self.view addSubview:self.floatTypeLabel];
    
    [self.floatTypeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.top.mas_equalTo(self.view).offset(84 * SCREENH6);
    }];
    
    
    UILabel * floatTypeValueLabel = [UILabel new];
    
    floatTypeValueLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    floatTypeValueLabel.textColor = RED_COLOR;
    floatTypeValueLabel.textAlignment = NSTextAlignmentCenter;
//    floatTypeValueLabel.text = @"+$8.88";
    
    self.floatTypeValueLabel = floatTypeValueLabel;
    
    [self.view addSubview:self.floatTypeValueLabel];
    
    [self.floatTypeValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.centerY.mas_equalTo(self.floatTypeLabel);
    }];
    
    
    UILabel * buyTypeLabel = [UILabel new];
    
    buyTypeLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    buyTypeLabel.textColor = RED_COLOR;
    buyTypeLabel.textAlignment = NSTextAlignmentRight;
//    buyTypeLabel.text = @"买涨";
    
    self.buyTypeLabel = buyTypeLabel;
    
    [self.view addSubview:self.buyTypeLabel];
    
    [self.buyTypeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.centerY.mas_equalTo(self.floatTypeValueLabel);
    }];
    
    
    UIView * oneLineView = [UIView new];
    
    oneLineView.backgroundColor = [UIColor whiteColor];
    oneLineView.alpha = 0.1;
    
    self.oneLineView = oneLineView;
    
    [self.view addSubview:self.oneLineView];
    
    [self.oneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(1 * SCREENH6);
        make.top.mas_equalTo(self.buyTypeLabel.mas_bottom).offset(15 * SCREENH6);
    }];//trandInfoLabel
    
    
    UILabel * trandInfoLabel = [UILabel new];
    
    trandInfoLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    trandInfoLabel.textColor = ColorWithRGB(0xa2a2a2);
    trandInfoLabel.textAlignment = NSTextAlignmentLeft;
    trandInfoLabel.text = @"订单信息";
    
    self.trandInfoLabel = trandInfoLabel;
    
    [self.view addSubview:self.trandInfoLabel];
    
    [self.trandInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.floatTypeLabel);
        make.top.mas_equalTo(self.oneLineView.mas_bottom).offset(10 * SCREENH6);
    }];
    
    
    
    UILabel * trandTypeLabel = [UILabel new];
    
    trandTypeLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14 * SCREENH6];
    trandTypeLabel.textColor = ColorWithRGB(0xa2a2a2);
    trandTypeLabel.textAlignment = NSTextAlignmentLeft;
    trandTypeLabel.text = @"交易品种";
    
    self.trandTypeLabel = trandTypeLabel;
    
    [self.view addSubview:self.trandTypeLabel];
    
    [self.trandTypeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.trandInfoLabel);
        make.top.mas_equalTo(self.trandInfoLabel.mas_bottom).offset(15 * SCREENH6);
    }];
    
    
    UILabel * trandTypeValueLabel = [UILabel new];
    
    trandTypeValueLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14 * SCREENH6];
    trandTypeValueLabel.textColor = ColorWithRGB(0xffffff);
    trandTypeValueLabel.textAlignment = NSTextAlignmentLeft;
//    trandTypeValueLabel.text = @"USDCNH";
    
    self.trandTypeValueLabel = trandTypeValueLabel;
    
    [self.view addSubview:self.trandTypeValueLabel];
    
    [self.trandTypeValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.trandTypeLabel.mas_trailing).offset(10 * SCREENH6);
        make.centerY.mas_equalTo(self.trandTypeLabel);
    }];
    
    
    UILabel * orderNumberLabel = [UILabel new];
    
    orderNumberLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14 * SCREENH6];
    orderNumberLabel.textColor = ColorWithRGB(0xa2a2a2);
    orderNumberLabel.textAlignment = NSTextAlignmentLeft;
    orderNumberLabel.text = @"订单编号";
    
    self.orderNumberLabel = orderNumberLabel;
    
    [self.view addSubview:self.orderNumberLabel];
    
    [self.orderNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.trandTypeValueLabel.mas_trailing).offset(85 * SCREENH6);
        make.centerY.mas_equalTo(self.trandTypeValueLabel);
    }];
    
    
    UILabel * orderNumberValueLabel = [UILabel new];
    
    orderNumberValueLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14 * SCREENH6];
    orderNumberValueLabel.textColor = ColorWithRGB(0xa2a2a2);
    orderNumberValueLabel.textAlignment = NSTextAlignmentLeft;
//    orderNumberValueLabel.text = @"20147";
    
    self.orderNumberValueLabel = orderNumberValueLabel;
    
    [self.view addSubview:self.orderNumberValueLabel];
    
    [self.orderNumberValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.orderNumberLabel.mas_trailing).offset(10 * SCREENH6);
        make.centerY.mas_equalTo(self.orderNumberLabel);
    }];
    
    
    UILabel * pendingTypeLabel = [UILabel new];
    
    pendingTypeLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14 * SCREENH6];
    pendingTypeLabel.textColor = ColorWithRGB(0xa2a2a2);
    pendingTypeLabel.textAlignment = NSTextAlignmentLeft;
    pendingTypeLabel.text = @"交易手数";
    
    self.pendingTypeLabel = pendingTypeLabel;
    
    [self.view addSubview:self.pendingTypeLabel];
    
    [self.pendingTypeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.trandTypeLabel);
        make.top.mas_equalTo(self.trandTypeLabel.mas_bottom).offset(15 * SCREENH6);
    }];
    
    
    UILabel * pendingTypeValueLabel = [UILabel new];
    
    pendingTypeValueLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14 * SCREENH6];
    pendingTypeValueLabel.textColor = ColorWithRGB(0xa2a2a2);
    pendingTypeValueLabel.textAlignment = NSTextAlignmentLeft;
//    pendingTypeValueLabel.text = @"0.01";
    
    self.pendingTypeValueLabel = pendingTypeValueLabel;
    
    [self.view addSubview:self.pendingTypeValueLabel];
    
    [self.pendingTypeValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.trandTypeValueLabel);
        make.centerY.mas_equalTo(self.pendingTypeLabel);
    }];
    
    UILabel * pendingPriceLabel = [UILabel new];
    
    pendingPriceLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14 * SCREENH6];
    pendingPriceLabel.textColor = ColorWithRGB(0xa2a2a2);
    pendingPriceLabel.textAlignment = NSTextAlignmentLeft;
    pendingPriceLabel.text = @"隔夜利息";
    
    self.pendingPriceLabel = pendingPriceLabel;
    
    [self.view addSubview:self.pendingPriceLabel];
    
    [self.pendingPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.orderNumberLabel);
        make.centerY.mas_equalTo(self.pendingTypeValueLabel);
    }];
    
    
    UILabel * pendingPriceValueLabel = [UILabel new];
    
    pendingPriceValueLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14 * SCREENH6];
    pendingPriceValueLabel.textColor = ColorWithRGB(0xa2a2a2);
    pendingPriceValueLabel.textAlignment = NSTextAlignmentLeft;
//    pendingPriceValueLabel.text = @"$10.00";
    
    self.pendingPriceValueLabel = pendingPriceValueLabel;
    
    [self.view addSubview:self.pendingPriceValueLabel];
    
    [self.pendingPriceValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.orderNumberValueLabel);
        make.centerY.mas_equalTo(self.pendingPriceLabel);
    }];
    
    
    UILabel * buyPriceLabel = [UILabel new];
    
    buyPriceLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14 * SCREENH6];
    buyPriceLabel.textColor = ColorWithRGB(0xa2a2a2);
    buyPriceLabel.textAlignment = NSTextAlignmentLeft;
    buyPriceLabel.text = @"买入价格";
    
    self.buyPriceLabel = buyPriceLabel;
    
    [self.view addSubview:self.buyPriceLabel];
    
    [self.buyPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.pendingTypeLabel);
        make.top.mas_equalTo(self.pendingTypeLabel.mas_bottom).offset(15 * SCREENH6);
    }];
    
    
    UILabel * buyPriceValueLabel = [UILabel new];
    
    buyPriceValueLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14 * SCREENH6];
    buyPriceValueLabel.textColor = ColorWithRGB(0xa2a2a2);
    buyPriceValueLabel.textAlignment = NSTextAlignmentLeft;
//    buyPriceValueLabel.text = @"6.78921";
    
    self.buyPriceValueLabel = buyPriceValueLabel;
    
    [self.view addSubview:self.buyPriceValueLabel];
    
    [self.buyPriceValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.pendingTypeValueLabel);
        make.centerY.mas_equalTo(self.buyPriceLabel);
    }];
    
    
    UILabel * nowPriceLabel = [UILabel new];
    
    nowPriceLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14 * SCREENH6];
    nowPriceLabel.textColor = ColorWithRGB(0xa2a2a2);
    nowPriceLabel.textAlignment = NSTextAlignmentLeft;
    nowPriceLabel.text = @"现在价格";
    
    self.nowPriceLabel = nowPriceLabel;
    
    [self.view addSubview:self.nowPriceLabel];
    
    [self.nowPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.orderNumberLabel);
        make.centerY.mas_equalTo(self.buyPriceValueLabel);
    }];
    
    
    UILabel * nowPriceValueLabel = [UILabel new];
    
    nowPriceValueLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14 * SCREENH6];
    nowPriceValueLabel.textColor = ColorWithRGB(0xa2a2a2);
    nowPriceValueLabel.textAlignment = NSTextAlignmentLeft;
//    nowPriceValueLabel.text = @"6.78921";
    
    self.nowPriceValueLabel = nowPriceValueLabel;
    
    [self.view addSubview:self.nowPriceValueLabel];
    
    [self.nowPriceValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.orderNumberValueLabel);
        make.centerY.mas_equalTo(self.nowPriceLabel);
    }];
    
    
    UILabel * stopLosePriceLabel = [UILabel new];
    
    stopLosePriceLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14 * SCREENH6];
    stopLosePriceLabel.textColor = ColorWithRGB(0xa2a2a2);
    stopLosePriceLabel.textAlignment = NSTextAlignmentLeft;
    stopLosePriceLabel.text = @"止损价格";
    
    self.stopLosePriceLabel = stopLosePriceLabel;
    
    [self.view addSubview:self.stopLosePriceLabel];
    
    [self.stopLosePriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.buyPriceLabel);
        make.top.mas_equalTo(self.buyPriceLabel.mas_bottom).offset(15 * SCREENH6);
    }];
    
    
    UILabel * stopLosePriceValueLabel = [UILabel new];
    
    stopLosePriceValueLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14 * SCREENH6];
    stopLosePriceValueLabel.textColor = ColorWithRGB(0xa2a2a2);
    stopLosePriceValueLabel.textAlignment = NSTextAlignmentLeft;
//    stopLosePriceValueLabel.text = @"6.78921";
    
    self.stopLosePriceValueLabel = stopLosePriceValueLabel;
    
    [self.view addSubview:self.stopLosePriceValueLabel];
    
    [self.stopLosePriceValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.buyPriceValueLabel);
        make.centerY.mas_equalTo(self.stopLosePriceLabel);
    }];
    
    
    UILabel * stopWinPriceLabel = [UILabel new];
    
    stopWinPriceLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14 * SCREENH6];
    stopWinPriceLabel.textColor = ColorWithRGB(0xa2a2a2);
    stopWinPriceLabel.textAlignment = NSTextAlignmentLeft;
    stopWinPriceLabel.text = @"止盈价格";
    
    self.stopWinPriceLabel = stopWinPriceLabel;
    
    [self.view addSubview:self.stopWinPriceLabel];
    
    [self.stopWinPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.nowPriceLabel);
        make.centerY.mas_equalTo(self.stopLosePriceValueLabel);
    }];
    
    
    UILabel * stopWinPriceValueLabel = [UILabel new];
    
    stopWinPriceValueLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14 * SCREENH6];
    stopWinPriceValueLabel.textColor = ColorWithRGB(0xa2a2a2);
    stopWinPriceValueLabel.textAlignment = NSTextAlignmentLeft;
//    stopWinPriceValueLabel.text = @"6.78921";
    
    self.stopWinPriceValueLabel = stopWinPriceValueLabel;
    
    [self.view addSubview:self.stopWinPriceValueLabel];
    
    [self.stopWinPriceValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.nowPriceValueLabel);
        make.centerY.mas_equalTo(self.stopWinPriceLabel);
    }];
    
    
    UILabel * pendingTimeLabel = [UILabel new];
    
    pendingTimeLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14 * SCREENH6];
    pendingTimeLabel.textColor = ColorWithRGB(0xa2a2a2);
    pendingTimeLabel.textAlignment = NSTextAlignmentLeft;
    pendingTimeLabel.text = @"买入时间";
    
    self.pendingTimeLabel = pendingTimeLabel;
    
    [self.view addSubview:self.pendingTimeLabel];
    
    [self.pendingTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.stopLosePriceLabel);
        make.top.mas_equalTo(self.stopLosePriceLabel.mas_bottom).offset(15 * SCREENH6);
    }];
    
    
    UILabel * pendingTimeValueLabel = [UILabel new];
    
    pendingTimeValueLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14 * SCREENH6];
    pendingTimeValueLabel.textColor = ColorWithRGB(0xa2a2a2);
    pendingTimeValueLabel.textAlignment = NSTextAlignmentLeft;
//    pendingTimeValueLabel.text = @"2017.07.20 17:33:00";
    
    self.pendingTimeValueLabel = pendingTimeValueLabel;
    
    [self.view addSubview:self.pendingTimeValueLabel];
    
    [self.pendingTimeValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.stopLosePriceValueLabel);
        make.centerY.mas_equalTo(self.pendingTimeLabel);
    }];
    
    
    UIView * twoLineView = [UIView new];
    
    twoLineView.backgroundColor = [UIColor whiteColor];
    twoLineView.alpha = 0.1;
    
    self.twoLineView = twoLineView;
    
    [self.view addSubview:self.twoLineView];
    
    [self.twoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(1 * SCREENH6);
        make.top.mas_equalTo(self.pendingTimeValueLabel.mas_bottom).offset(15 * SCREENH6);
    }];
    
    
    
    UIButton * settingButton = [UIButton new];
    
    [settingButton setImage:[UIImage imageNamed:@"设置盈亏1"] forState:UIControlStateNormal];
    [settingButton addTarget:self action:@selector(didSettingButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.settingButton = settingButton;
    
    [self.view addSubview:self.settingButton];
    
    [self.settingButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.bottom.mas_equalTo(self.view).offset(-40 * SCREENH6);
        make.width.mas_equalTo(167 * SCREENH6);
        make.height.mas_equalTo(35 * SCREENH6);
    }];
    
    
    
    UIButton * cancelButton = [UIButton new];
    
    [cancelButton setImage:[UIImage imageNamed:@"平仓1"] forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(didCancelButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.cancelButton = cancelButton;
    
    [self.view addSubview:self.self.cancelButton];
    
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.settingButton);
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.width.mas_equalTo(167 * SCREENH6);
        make.height.mas_equalTo(35 * SCREENH6);
    }];
    
}


- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message;
{
    
    
    NSData *jsonData = [message dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    
    NSArray * array = dic[@"quote"];
    
    //        NSLog(@"==%@",array);
    
    for (NSDictionary * productInfoDict in array) {
        
        NSString * name = [NSString stringWithFormat:@"%@",productInfoDict[@"symbol"]];
        
        if ([self.trandTypeValueLabel.text isEqualToString:name]) {
            
            NSString * bid = [NSString stringWithFormat:@"%@",productInfoDict[@"bid"]];
            NSString * ask = [NSString stringWithFormat:@"%@",productInfoDict[@"ask"]];
            
            if ([self.typeString isEqualToString:@"Buy"]) {
               
                if (bid.length <= 7) {
                    self.nowPriceValueLabel.text = bid;
                }else{
                    bid = [bid substringToIndex:8];
                    self.nowPriceValueLabel.text = bid;
                }
            }else{
               
                if (ask.length <= 7) {
                    self.nowPriceValueLabel.text = ask;
                }else{
                    ask = [ask substringToIndex:8];
                    self.nowPriceValueLabel.text = ask;
                }

                
            }
        }
    }
    
}

//点击设置盈亏
- (void)didSettingButton{
    
    self.hidesBottomBarWhenPushed = YES;
    
    ChangeViewController * changeVC = [ChangeViewController new];
    
    changeVC.positionId = self.positionId;
    changeVC.pirceString = self.buyPriceValueLabel.text;
    changeVC.typeString = self.typeString;
    changeVC.type = @"2";
    
    [self.navigationController pushViewController:changeVC animated:YES];
    
}



//点击平仓
- (void)didCancelButton{
    [_countDownTimer invalidate];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSDictionary * parameters = @{@"positionId":self.positionId,@"volume":self.pendingTypeValueLabel.text};
    
    [[NetworkTool sharedTools] POST:@"symbols/close" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        NSString * code = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        
        if ([code isEqualToString:@"0"]) {
            
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:@"恭喜你，平仓成功！"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                      [self.navigationController popViewControllerAnimated:YES];
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseObject[@"msg"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"服务器内部错误，请稍后再试。"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  //响应事件
                                                                  
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }];
    
    
}



- (void)backToAccountView{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidDisappear:(BOOL)animated{
    
    [_countDownTimer invalidate];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
